package com.papper.common.service.sys;

import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.papper.common.bean.sys.SysUser;
import com.papper.common.mapper.sys.SysUserMapper;
import com.papper.common.service.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
 * The type Sys user service.
 */
@Service("sysUserService")
public class SysUserService extends ServiceImpl<SysUserMapper, SysUser> {

	@Resource
	SysLimitService      sysLimitService;
	@Autowired
	SysGroupService      sysGroupService;
	@Autowired
	SysPageConfigService sysPageConfigService;

	/**
	 * 匹配层级路径.
	 *
	 * @param sysUser the sys user
	 */
	public void matchFamily(SysUser sysUser) {
		SysUser parent = findById(sysUser.getParentId());
		String  family;
		if (parent != null) {
			family = parent.getFamily() + "," + sysUser.getId();
		} else {
			family = "0," + sysUser.getId();
		}

		SysUser upd = new SysUser();
		upd.setId(sysUser.getId());
		upd.setFamily(family);
		update(upd);
		sysUser.setFamily(family);
	}

	/**
	 * 匹配子商户数量.
	 *
	 * @param sysUser the sys user
	 */
	public void matchChild(SysUser sysUser) {
		if (sysUser == null || sysUser.getId().intValue() == 0) {
			return;
		}
		// 子商户数维护
		Example example = new Example(SysUser.class);
		example.createCriteria().andEqualTo("parentId", sysUser.getId());
		Integer userChild = count(example);
		// 数据更新
		SysUser upd = new SysUser();
		upd.setId(sysUser.getId());
		upd.setUserChild(userChild == null ? 0 : userChild.intValue());
		update(upd);
		sysUser.setUserChild(userChild == null ? 0 : userChild.intValue());
	}

	/**
	 * 获取角色用户.
	 *
	 * @param groupId the group id
	 * @return the list
	 */
	public List<SysUser> findByGroupId(Long groupId) {
		Example example = new Example(SysUser.class);
		example.createCriteria().andEqualTo("groupId", groupId);
		return find(example);
	}

	/**
	 * 更新用户密码.
	 *
	 * @param id       the id
	 * @param password the password
	 */
	public void updatePassword(Long id, String password) {
		SysUser upd = new SysUser();
		upd.setId(id);
		upd.setLoginPass(password);
		upd.setModifyTime(new Date());
		update(upd);
	}

	/**
	 * 删除系统下所有用户.
	 *
	 * @param systemId the system id
	 */
	public void deleteBySystem(Long systemId) {
		Example example = new Example(SysUser.class);
		example.createCriteria().andEqualTo("systemId", systemId);
		List<SysUser> lstTree = find(example);
		Set<Long>     userIds = Sets.newHashSet();
		Optional.ofNullable(lstTree).orElse(Lists.newArrayList()).forEach(user -> {
			userIds.add(user.getId());
		});
		sysLimitService.delLimitedByUserIds(Joiner.on(",").skipNulls().join(userIds));
		delete(example);
	}

	/**
	 * 删除层级用户信息.
	 */
	public void deleteUserTree(SysUser sysUser, boolean self) {
		Example          example  = new Example(SysUser.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.orLike("family", sysUser.getFamily() + ",%");
		if (self) {
			criteria.orEqualTo("family", sysUser.getFamily());
		}
		// 删除用户创建角色
		List<SysUser> lstTree = find(example);
		Set<Long>     userIds = Sets.newHashSet();
		Optional.ofNullable(lstTree).orElse(Lists.newArrayList()).forEach(user -> {
			userIds.add(user.getId());
			sysPageConfigService.deleteByUser(user.getId());
		});
		// 删除用户创建角色
		sysGroupService.deleteByUserIds(Joiner.on(",").skipNulls().join(userIds));
		// 删除用户级别权限信息
		sysLimitService.delLimitedByUserIds(Joiner.on(",").skipNulls().join(userIds));
		// 删除用户及子用户
		delete(example);
	}

	/**
	 * 更新用户权限类型.
	 *
	 * @param id        the id
	 * @param limitType the limit type
	 */
	public void updateLimitType(Long id, Integer limitType) {
		SysUser upd = new SysUser();
		upd.setId(id);
		upd.setLimitType(limitType);
		upd.setModifyTime(new Date());
		update(upd);
	}
}